package com.joejag.mavenstats.server.dao;

import com.joejag.mavenstats.client.dto.ProjectDTO;

import java.io.File;
import java.util.*;

public class MockProjectsDao implements ProjectsDao
{
  public static final String MOCK_XML_URL = new File("test/xml/").getAbsolutePath();

  private static Map<String, List<ProjectDTO>> dataStore;

  public MockProjectsDao()
  {
    dataStore = new HashMap<String, List<ProjectDTO>>();

    List<ProjectDTO> staticProjects = new ArrayList<ProjectDTO>();
    staticProjects.add(new ProjectDTO(MOCK_XML_URL + "/commod_cli.xml", "TDMD", "TDMD Client"));
    staticProjects.add(new ProjectDTO(MOCK_XML_URL + "/commod_ser.xml", "TDMD", "TDMD Server"));
    dataStore.put("TDMD", staticProjects);

    List<ProjectDTO> tdmdProjects = new ArrayList<ProjectDTO>();
    tdmdProjects.add(new ProjectDTO(MOCK_XML_URL + "/norway.xml", "Norway", "DHS - Server"));
    tdmdProjects.add(new ProjectDTO(MOCK_XML_URL + "/norway2.xml", "Norway", "DHS - Client"));
    dataStore.put("Norway", tdmdProjects);
  }

  public MockProjectsDao(Map<String, List<ProjectDTO>> dataStore)
  {
    this.dataStore = dataStore;
  }

  public Map getByTeam()
  {
    return dataStore;
  }

  public List getTeamNames()
  {
    return new ArrayList(dataStore.keySet());
  }

  public List getTeamProjects(String teamName)
  {
    return dataStore.get(teamName);
  }

  public void addProject(ProjectDTO project)
  {
    addProject(project, -1);
  }

  public void addProject(ProjectDTO project, int position)
  {
    throwExceptionIfUrlAlreadyExists(project);

    if (this.getTeamNames().contains(project.teamname))
    {
      List list = this.getTeamProjects(project.teamname);
      if (position == -1)
        list.add(project);
      else
        list.add(position, project);
    } else
    {
      List staticProjects = new ArrayList();
      staticProjects.add(project);
      dataStore.put(project.teamname, staticProjects);
    }
  }

  private void throwExceptionIfUrlAlreadyExists(ProjectDTO project)
  {
    Iterator teamNameIter = this.getTeamNames().iterator();
    while (teamNameIter.hasNext())
    {
      List list = this.getTeamProjects(teamNameIter.next().toString());
      Iterator projectDTOiter = list.iterator();
      while (projectDTOiter.hasNext())
      {
        ProjectDTO storedProject = (ProjectDTO) projectDTOiter.next();
        if (storedProject.projectURL.equals(project.projectURL))
          throw new RuntimeException("Project already exists: " + storedProject.teamname + "::" + storedProject.projectName);
      }
    }
  }

  public int delete(ProjectDTO deletedProject)
  {
    int position = -1;

    Iterator teamNameIter = dataStore.keySet().iterator();
    while (teamNameIter.hasNext())
    {
      List list = (List) dataStore.get(teamNameIter.next().toString());
      Iterator projectDTOiter = list.iterator();
      while (projectDTOiter.hasNext())
      {
        ProjectDTO storedProject = (ProjectDTO) projectDTOiter.next();
        if (storedProject.projectURL.equals(deletedProject.projectURL))
        {
          position = list.indexOf(storedProject);
          projectDTOiter.remove();
        }
      }

      if (list.size() == 0)
        teamNameIter.remove();
    }

    return position;
  }

  public void edit(ProjectDTO edittedProject)
  {
    int position = this.delete(edittedProject);
    addProject(edittedProject, position);
  }
}
